package models

import (
	"fmt"
	"log"
	"rongyy/src/app"
)

type CustomNoticeModel struct {
	BaseModel `xorm:"extends"`
	Title     string `json:"title"`
	Content   string `json:"content"`
}

type ReqNotice struct {
	Limit  int `json:"limit"`
	Offset int `json:"offset"`
}

func (rm *CustomNoticeModel) TableName() string {
	return "custom_notice"
}

func (rm *CustomNoticeModel) Create() error {
	_, err := app.DB().Insert(rm)
	if err != nil {
		log.Println(err)
		return err
	}
	return nil
}

func (rm *CustomNoticeModel) Update() error {
	_, err := app.DB().ID(rm.Id).Update(rm)
	if err != nil {
		log.Println(err)
		return err
	}
	return nil
}

func (rm *CustomNoticeModel) GetById(id int64) (*CustomNoticeModel, error) {
	cnm := &CustomNoticeModel{}
	has, err := app.DB().ID(id).Get(cnm)
	if err != nil {
		return nil, err
	}
	if !has {
		return nil, fmt.Errorf("信息不存在")
	}

	return cnm, nil
}

func (rm *CustomNoticeModel) Query(req *ReqNotice) ([]CustomNoticeModel, int64, error) {
	regs := make([]CustomNoticeModel, 0)
	//local, _ := time.LoadLocation("Asia/Shanghai")
	//t := time.Now().In(local).Format("2006-01-02 15:04:05")
	s := app.DB().Table("custom_notice")
	//if req.PatientId != "" {
	//	pid := fmt.Sprintf("%%%s%%", req.PatientId)
	//	s.Where("patient_id like ?", pid)
	//}
	//
	//if req.OrderSn != "" {
	//	orderSn := fmt.Sprintf("%%%s%%", req.OrderSn)
	//	s.Where("order_sn like ?", orderSn)
	//}
	//
	//if req.HisCode != "" {
	//	hisCode := fmt.Sprintf("%%%s%%", req.HisCode)
	//	s.Where("his_code like ?", hisCode)
	//}
	total, err := s.Limit(req.Limit, req.Offset).Desc("created_at").FindAndCount(&regs)
	if err != nil {
		return nil, 0, err
	}
	return regs, total, err
}
